﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QXBook.Service;
using QXBook.Model;
using QXBook.Utils;

namespace Web.ctrl
{
    public partial class UserLoginCtrl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataBind();
            }
            lbtnLogin.Click += new EventHandler(lbtnLogin_Click);
        }

        void lbtnLogin_Click(object sender, EventArgs e)
        {
            LoginMethod();
        }

        /// <summary>
        /// 用户登陆
        /// </summary>
        private void LoginMethod()
        {
            string adminname = CommonUtils.Post("username");
            string password = CommonUtils.Post("password");

            if (string.IsNullOrEmpty(adminname) || string.IsNullOrEmpty(password))
            {
                ResponseErch("请输入用户名和密码");
                return;
            }

            string rtn = string.Empty;
            try
            {
                rtn = ServiceFactory.CreateInstance<UsersService>().UserLogin(adminname, CommonUtils.MD5(password));

            }
            catch (Exception ex)
            {
                ResponseErch(ex.Message.ToString());
                return;
            }

            if (rtn != MsgConst.OK)
            {
                phNoLogin.Visible = true;
                phIsLogin.Visible = false;
                ResponseErch(rtn);
            }
            else
            {
                DataBind();
                phNoLogin.Visible = false;
                phIsLogin.Visible = true;
            }
        }

        protected void ResponseErch(string str)
        {
            this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "message", "<script type='text/javascript'>alert('" + str + "')</script>");
        }

        protected void ResponseErch(string str, string okmessage)
        {
            if (!string.IsNullOrEmpty(okmessage))
            {
                this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "message", "<script type='text/javascript'>alert('" + okmessage + "')</script>");
            }
            else
            {
                this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "message", "<script type='text/javascript'>alert('" + str + "')</script>");
            }
        }

        public override void DataBind()
        {
            #region 用户
            UsersInfo userInfo = ServiceFactory.CreateInstance<UsersService>().GetUsersInfo();

            if (userInfo != null)
            {
                phNoLogin.Visible = false;
                phIsLogin.Visible = true;

                litUserName.Text = userInfo.UserName;
            }
            else
            {
                phNoLogin.Visible = true;
                phIsLogin.Visible = false;
            }
            #endregion
        }
    }
}